package storm.Processors;

import java.util.Vector;

import storm.Tasks.Task;

public class LEATCache extends Cache {
	Vector <storm.Tasks.Task>Tasks;
	String nameCP ;
	
	public LEATCache (){
		super();
		this.Tasks = new Vector<Task>();
		 nameCP = this.toString()+"_CP";
	}
	
	double getXi(Task T){
		double res =0;
		
		//long x = (long)T.getEET() / (long)T.getAET() ;
		int Xi = T.getOwnFieldIntValue("Xi");
		int Li = T.getOwnFieldIntValue("Li");
		double Tpen = Double.parseDouble(this.getOwnFieldStringValue("BurstModeAccesTime"));
	//	res = (Xi/(1-Math.exp(-Li*T.getWCET())  ))  * (1.0-Math.exp(-Li*(double)T.getEET())); ;
		res =  (Xi/(1-Math.exp(-Li*T.getWCET())  ))*(1.0-Math.exp(-Li*(double)T.getEET()/1000*this.Kernel.getprecision())); ;
		String r = T.getOwnFieldStringValue("RWratio");

	  double rw_ratio = Double.valueOf(r);
		res = res *(1+ rw_ratio);
		res = Tpen * res; 
		//res = (Xi / (1-Math.exp(-Li*(T.getWCET())*(1.0-Math.exp((double)-Li*(double)T.getEET()*this.Kernel.precision/1000.0 ));
		//res = (double) (T.getEET()*this.Kernel.precision)/1000; 
		//System.out.println( T.getEET()*this.Kernel.precision + "  x " +  T.getAET()*this.Kernel.precision + "   " + res);
		return res;
		
	}
	public int havePenality(Task T) {
		// TODO Auto-generated method stub
		//System.out.println(");
		if (this.Tasks.contains(T)){
			
			double value = this.getXi( T);
			if (T.getET()==0) {
				T.setOwnFieldIntValue(this.nameCP, 0);
			}
			int compteur = T.getOwnFieldIntValue(this.nameCP);
			if( (value - compteur) > 1) {
				int reste = (int)(compteur+1);
				T.setOwnFieldIntValue(this.nameCP,reste);
				//System.out.println(reste );
				return 1;
				
			}
			
		}
		else {
			this.Tasks.add(T);
			T.setOwnFieldIntValue(this.nameCP,0);
		}
		
		return 0;
	}

	
}
